package net.sahv.bdyz.api;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import net.sahv.bdyz.model.Member;
import net.sahv.bdyz.model.Theme;
import net.sahv.bdyz.service.LanguageService;
import net.sahv.bdyz.service.MemberService;
import net.sahv.bdyz.service.PatternService;
import net.sahv.bdyz.service.ThemeService;
import net.sahv.bdyz.util.LoadCfgUtil;
import net.sahv.bdyz.util.PageInfo;

/**
 * @author 作者 : 任凯伟
 * @date 创建时间：2016年8月3日 上午9:46:36
 * @version 1.0
 * @see 主题
 */
@Controller("apiThemeController")
@RequestMapping("api/theme")
public class ThemeController {
	
	@Autowired
	private ThemeService themeService;// 主题
	@Autowired
	private LanguageService languageService;// 语言
	@Autowired
	private MemberService memberService;// 会员
	@Autowired
	private PatternService patternService;// 花型
	/**
	 * @author 作者 : 任凯伟
	 * @date 创建时间：2016年8月3日 上午11:29:02
	 * @version 1.0
	 * @see 查询主题列表
	 * @param
	 * @parameter
	 * @since
	 * @return
	 */
	@RequestMapping(value = "/main", method = RequestMethod.GET)
	public String subjectClassification(Model model) {
		String language = languageService.getCurrentLanguage();// 获取当前语言
		List<Theme> themeList = themeService.findThemeList();// 获取主题列表
		if ("en".equals(language)) {// 如果是英文 名称全部返回英文 页面接受用info字段
			for (Theme th : themeList) {
				th.setInfo(th.getMessage1());
				th.setName(th.getNameEn());
			}
		}
		model.addAttribute("list", themeList);
		return "phone/theme/theme";
	}
	
	/**
	 * 
	 * @author 作者 : 任凯伟
	 * @date 创建时间：2016年8月10日 上午10:54:24
	 * @version 1.0
	 * @see 主题详情（分页）
	 * @param
	 * @parameter
	 * @since
	 * @return
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	@RequestMapping(value = "/details", method = RequestMethod.GET)
	public String themeDetails(Model model, PageInfo pageInfo, Integer id) throws FileNotFoundException, IOException {
		Map<String,String> mapUrls=LoadCfgUtil.loadCfg();
		String imgParent=mapUrls.get("targetUrl.img");
		if (pageInfo == null) {
			pageInfo = new PageInfo();
		}
		pageInfo.setPageSize(20);
		if (pageInfo.getPage() < 2 || pageInfo.getPage() >= pageInfo.getTotalPage()) {
			pageInfo.setCount(0);
		}
		if (id != null) {
			String language = languageService.getCurrentLanguage();// 获取当前语言
			Theme theme = themeService.selectByPrimaryKey(id);// 主题信息
			if ("en".equals(language) && theme != null) {// 如果是英文 名称全部返回英文
															// 页面接受用info字段
				theme.setInfo(theme.getMessage1());
			}
			String where = " p.P_IS_DELETE=0 and p.P_SEND_STATE=1 and p.P_BUYOUT_STATE=0 and p.T_ID="
					+ id;
			String orderStr = " order by p.P_SEND_TIME desc";
			Member member = memberService.getCurrent();// 获取当前登录会员
			int mId=0;
			if (member != null) {
				mId = member.getmId();
			}
			// 花型列表 分页
			List<Map<String, Object>> pList = patternService.getByPage(pageInfo, where, orderStr, mId);
			if (pList.size() > 0) {
				pageInfo.setCount(Integer.parseInt(pList.get(0).get("myCount").toString()));
				pageInfo.setTotalPage((int) Math
						.ceil(Integer.parseInt(pList.get(0).get("myCount").toString()) / pageInfo.getPageSize()));
				for(Map<String, Object> pListOne:pList){
					String pImg=(String) pListOne.get("pImg");
					pListOne.remove("pImg");
					pListOne.put("pImg", imgParent+pImg);
				}
				
			}
			if (pageInfo.getPage() > pageInfo.getTotalPage()) {
				pageInfo.setPage(pageInfo.getTotalPage());
			}
			model.addAttribute("theme", theme);
			model.addAttribute("member", member);
			model.addAttribute("pList", pList);
			model.addAttribute("page", pageInfo);
		}
		return "phone/theme/themeDetail";
	}
	
}
 